package cn.lg.soar.generator.config;

import cn.lg.soar.core.model.AccessTokenProps;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Map;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2023/12/30 17:03
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ImportAutoConfiguration({
        AccessTokenProps.class,
})
@Configuration
public class Config {

    @Autowired
    private DynamicDataSourceProperties sourceProperties;

    @Bean
    public DruidDataSource dataSource() {
        Map<String, DataSourceProperty> datasource = sourceProperties.getDatasource();
        DataSourceProperty dataSourceProperty = datasource.get(sourceProperties.getPrimary());

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(dataSourceProperty.getUrl());
        druidDataSource.setUsername(dataSourceProperty.getUsername());
        druidDataSource.setPassword(dataSourceProperty.getPassword());
        druidDataSource.setDriverClassName(dataSourceProperty.getDriverClassName());
        return druidDataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }

}
